﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Ataric.SuppressWP.Utilities
{
    public static class EnumerableExtensions
    {
        public static IEnumerable<TSource> Distinct<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> comparer)
        {
            return source.Distinct(new DynamicComparer<TSource, TResult>(comparer));
        }

        public static bool Contains<TSource, TResult>(this IEnumerable<TSource> source, TResult value, Func<TSource, TResult> selector)
        {
            foreach (TSource sourceItem in source)
            {
                TResult sourceValue = selector(sourceItem);
                if (sourceValue.Equals(value))
                    return true;
            }
            return false;
        }

        public static IEnumerable<TSource> DistinctBy<TSource, TValue>(this IEnumerable<TSource> source, Func<TSource, TValue> selector)
        {
            var comparer = ProjectionComparer<TSource>.CompareBy<TValue>(
                selector, EqualityComparer<TValue>.Default);
            return new HashSet<TSource>(source, comparer);
        }
    }

    public static class ProjectionComparer<TSource>
    {
        public static IEqualityComparer<TSource> CompareBy<TValue>(
            Func<TSource, TValue> selector)
        {
            return CompareBy<TValue>(selector, EqualityComparer<TValue>.Default);
        }

        public static IEqualityComparer<TSource> CompareBy<TValue>(
            Func<TSource, TValue> selector,
            IEqualityComparer<TValue> comparer)
        {
            return new ComparerImpl<TValue>(selector, comparer);
        }

        sealed class ComparerImpl<TValue> : IEqualityComparer<TSource>
        {
            private readonly Func<TSource, TValue> selector;
            private readonly IEqualityComparer<TValue> comparer;
            public ComparerImpl(
                Func<TSource, TValue> selector,
                IEqualityComparer<TValue> comparer)
            {
                if (selector == null) throw new ArgumentNullException("selector");
                if (comparer == null) throw new ArgumentNullException("comparer");
                this.selector = selector;
                this.comparer = comparer;
            }

            bool IEqualityComparer<TSource>.Equals(TSource x, TSource y)
            {
                if (x == null && y == null) return true;
                if (x == null || y == null) return false;
                return comparer.Equals(selector(x), selector(y));
            }

            int IEqualityComparer<TSource>.GetHashCode(TSource obj)
            {
                return obj == null ? 0 : comparer.GetHashCode(selector(obj));
            }
        }
    }

    public class DynamicComparer<T, TResult> : IEqualityComparer<T>
    {
        private readonly Func<T, TResult> _selector;

        public DynamicComparer(Func<T, TResult> selector)
        {
            _selector = selector;
        }

        public bool Equals(T x, T y)
        {

            TResult result1 = _selector(x);
            TResult result2 = _selector(y);
            return result1.Equals(result2);
        }

        public int GetHashCode(T obj)
        {
            if (obj == null) return 0;

            TResult result = _selector(obj);
            return result.GetHashCode();
        }
    }
}
